<template>
  <!-- 生产单添加--重做 -->
  <div class="contractInfo box">
    <el-card
      shadow="never"
      header="重做"
      class="item-background"
      style="width: 100%"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="addForm"
        label-width="auto"
        label-position="right"
      >
        <el-form-item label="生产单名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入生产单名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="生产负责人" prop="responsibleId">
          <el-select
            v-model="form.responsibleId"
            placeholder="请选择负责人"
            style="width: 100%"
            clearable
            filterable
          >
            <el-option
              v-for="item in userList"
              :key="item.id"
              :label="item.username"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="交货时间">
          <el-date-picker
            v-model="form.deliveryTime"
            type="date"
            placeholder="请选择交货时间"
            size="default"
            style="width: 100%"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="生产截止时间" prop="endTime">
          <el-date-picker
            v-model="form.endTime"
            type="date"
            placeholder="请选择生产截止时间"
            size="default"
            style="width: 100%"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="待重做" prop="adminId" style="width: 100%">
          <el-table
            ref="table"
            :data="productList"
            placeholder="暂无数据"
            stripe
            border
          >
            <el-table-column
              label="产品名称"
              prop="name"
              min-width="100"
            ></el-table-column>
            <el-table-column
              label="规格型号"
              prop="specification"
              min-width="100"
            ></el-table-column>
            <el-table-column
              label="材质"
              prop="material"
              min-width="100"
            ></el-table-column>
            <el-table-column
              label="备注"
              prop="remark"
              min-width="100"
            ></el-table-column>
            <el-table-column
              label="合同数量"
              prop="contractNum"
              width="200"
            ></el-table-column>
            <el-table-column
              label="退货数量"
              prop="recededNum"
              width="200"
            ></el-table-column>
            <el-table-column
              label="已处理数量"
              prop="handleNum"
              width="200"
            ></el-table-column>
            <el-table-column prop="recededNum" label="重做数量" width="250">
              <template #default="scope">
                <el-input-number
                  :precision="0"
                  v-model.number="scope.row.num"
                  placeholder="请输入重做数量"
                  style="width: 100%"
                ></el-input-number>
              </template>
            </el-table-column>
            <el-table-column
              label="单位"
              prop="unitName"
              width="200"
            ></el-table-column>
          </el-table>
        </el-form-item>
        <el-form-item label="备注" prop="remark" style="width: 100%">
          <el-input
            v-model="form.remark"
            clearable
            rows="3"
            type="textarea"
            placeholder="请输入备注内容"
          ></el-input>
        </el-form-item>
      </el-form>

      <div class="button">
        <el-button type="primary" @click="save" :loading="isSaveing"
          >保 存</el-button
        >
        <el-button @click="cencel">取 消</el-button>
      </div>
    </el-card>
  </div>
</template>
<script>
export default {
  name: 'reCreate',
  data () {
    return {
      isSaveing: false,
      selectContractLoading: false,
      selectLoading: false,
      //表单数据
      form: {
        produceId: '',
        contractId: '',
        rejectedId: '',
        responsibleId: '',//负责人id
        name: "",//生产单名称
        deliveryTime: '',//交货时间
        endTime: '',//生产截至时间
        remark: '',//备注
        source: 1,//0生产单 1生产单(重做) 2生产单维修
        produceProductsVOS: []
      },
      productList: [],
      userList: [], //用户列表
      //验证规则
      rules: {
        name: [
          { required: true, message: '请输入生产单名称' }
        ],
        responsibleId: [
          { required: true, message: '请选择生产负责人', trigger: 'change' }
        ],

      }
    }
  },
  mounted () {
    this.getDetail()
    this.getUserList()
    this.getContractConfig()
  },
  methods: {
    // 获取退货详情
    async getDetail () {
      let param = {
        id: this.$route.query.id,
      }
      let { data } = await this.$API.produce.returnProduct.getReturnProductById.get(param)
      this.productList = data.rejectedProductDTOS
      this.form.produceId = data.produceId
      this.form.contractId = data.contractId
      this.form.rejectedId = data.id
    },

    // 获取合同默认配置
    async getContractConfig () {
      let { data } = await this.$API.contract.contractConfig.getConfigDetail.post({ id: 1 })
      this.form.responsibleId = data.produceAdminId
    },

    // 获取所有用户列表
    async getUserList (val) {
      let data = {
        limit: 1000,
        page: 1,
        search: val
      }
      let res = await this.$API.user.getUserList.get(data)
      this.userList = res.data
    },

    // 搜索用户
    queryUser (val) {
      this.getUserList(val)
    },

    async save () {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {

          let userInfo = this.$TOOL.data.get('USER_INFO')
          this.form.departmentId = userInfo.departmentId
          this.form.adminId = userInfo.userId
          let list = []
          let hasNum = false
          this.productList.map(item => {
            if (item.num == 0 || !item.num) {
              this.$message.error(`请填写【${item.name}】的重做数量`)
              hasNum = true
            }
            if (item.num > (item.recededNum - item.handleNum)) {
              this.$message.error(`【${item.name}】可重做数量不足，请重新填写重做数量`)
              hasNum = true
            }
            list.push({
              quotesProductId: item.productId,
              num: item.num
            })
          })
          if (hasNum) return
          this.form.produceProductsVOS = list
          this.isSaveing = true
          let res = await this.$API.produce.addProduceOrder.post(this.form)

          this.isSaveing = false
          if (res.code == 200) {
            this.$emit('success', this.form, this.mode)
            this.closeSelectedTag(this.$route)
            this.$router.push({
              path: '/produce/produceOrderManagement'
            })
            this.$message.success("操作成功")
          }
        }
      })
    },

    // 取消添加
    cencel () {
      this.closeSelectedTag(this.$route)
      this.$router.push({
        path: '/produce/produceOrderManagement'
      })
    },
    //关闭tag
    closeSelectedTag (tag) {
      this.$store.commit("removeViewTags", tag)
      this.$store.commit("removeIframeList", tag)
      this.$store.commit("removeKeepLive", tag.name)
    },
  }

}

</script>
<style lang='scss' scoped>
.box {
  margin: 10px;
}

.formBox {
  padding: 30px;
  background: #fff;

  .el-form {
    width: 100%;
  }
}

.button {
  margin-top: 50px;
  padding-left: 60px;
}

:deep(.el-divider__text) {
  padding: 0 10px;
  font-size: 18px;
  // font-weight: 600;
  background-color: #f6f8f9;
}
</style>